home *** CD-ROM | disk | FTP | other *** search
/ EnigmA Amiga Run 1996 June / EnigmA AMIGA RUN 08 (1996)(G.R. Edizioni)(IT)[!][issue 1996-06][EARSAN CD VII].iso / earcd / c-lang / ezprintr.lha / EZPrinter.c next >
C/C++ Source or Header  |  1996-02-20  |  3KB  |  113 lines

  1. /*
  2. ** EZPrinter.c
  3. ** Version 1.00     By The Reaper
  4. **
  5. ** These little routines have been written to make it easier for you to
  6. ** use the printer device in your C programs without needing to worry about
  7. ** all the allocs/opens needed.
  8. **
  9. ** This was based on the RKRM example source but made to look a bit nicer
  10. ** and Dice 3.01 compatible.
  11. **
  12. ** Functions:
  13. **
  14. ** int open_printer(void)
  15. **     Open the printer device and setup msg ports etc. Returns 0 on success
  16. **
  17. ** void close_printer(void)
  18. **     Should be called even if open_printer fails as it frees all
  19. **     succesfull allocs made by open_printer()
  20. **
  21. ** void init_printer(void)
  22. **     Sends the init code to the printer
  23. **
  24. ** void send_text(char *text)
  25. **     Sends the string text to the printer. Note: Uses DoIO()
  26. **
  27. ** void queue_write(char *text)
  28. **     Same as send_text but uses SendIO() instead. Note: Don't forget to
  29. **     wait for it to return! You can make an Abort gadget by calling
  30. **     AbortIO() for the queued write.
  31. */
  32.  
  33. #define Prototype extern
  34.  
  35. #include <exec/types.h>
  36. #include <devices/printer.h>
  37. #include <devices/prtbase.h>
  38.  
  39. #include <clib/exec_protos.h>
  40. #include <clib/alib_protos.h>
  41. #include <clib/alib_stdio_protos.h>
  42.  
  43. /* Unions */
  44. union printerIO
  45. {
  46.     struct IOStdReq    ios;
  47.     struct IODRPReq    iodrp;
  48.     struct IOPrtCmdReq iopc;
  49. };
  50.  
  51. /* Globals */
  52. struct MsgPort *printMsgPort;
  53. union printerIO *pio;
  54.  
  55. /* Prototypes */
  56. Prototype int open_printer(void);
  57. Prototype void close_printer(void);
  58. Prototype void init_printer(void);
  59. Prototype void send_text(char *text);
  60. Prototype void queue_write(char *text);
  61.  
  62. int open_printer(void)
  63. {
  64.     if(printMsgPort = CreatePort(0L,0L))
  65.     {
  66.         if(pio = (union printerIO *)CreateExtIO(printMsgPort,sizeof(union printerIO)))
  67.         {
  68.             if(!(OpenDevice("printer.device",0L,(struct IORequest *)pio,0L)))
  69.             {
  70.                 return(0L);
  71.             }
  72.             else return(3L);
  73.         }
  74.         else return(2L);
  75.     }
  76.     else return(1L);
  77. }
  78. void close_printer(void)
  79. {
  80.     if(pio)
  81.     {
  82.         CloseDevice((struct IORequest *)pio);
  83.         DeleteExtIO((struct IORequest *)pio);
  84.     }
  85.     if(printMsgPort)
  86.     {
  87.         DeletePort(printMsgPort);
  88.     }
  89. }
  90. void init_printer(void)
  91. {
  92.     pio->ios.io_Command = CMD_WRITE;
  93.     pio->ios.io_Data = "\033#1";
  94.     pio->ios.io_Length = -1L;
  95.     
  96.     DoIO((struct IORequest *)pio);
  97. }
  98. void send_text(char *text)
  99. {
  100.     pio->ios.io_Command = CMD_WRITE;
  101.     pio->ios.io_Data = text;
  102.     pio->ios.io_Length = -1L;
  103.     
  104.     DoIO((struct IORequest *)pio);
  105. }
  106. void queue_write(char *text)
  107. {
  108.     pio->ios.io_Command = CMD_WRITE;
  109.     pio->ios.io_Data = text;
  110.     pio->ios.io_Length = -1L;
  111.     
  112.     SendIO((struct IORequest *)pio);
  113. }